<?php

namespace app\api\middleware;
use thans\jwt\facade\JWTAuth;
use thans\jwt\exception\JWTException;
use think\facade\Session;

class CheckToken
{
    public function handle($request, \Closure $next)
    {
        $userAgent = $request->header('userAgent');//1为ios  2安卓
        // OPTIONS请求直接返回
        if ($request->isOptions()) {
            return response();
        }
        try {
            $payload = JWTAuth::auth();
        }catch (JWTException $e) {
            return result([],$e->getMessage(),400);
            //return json($e->getMessage());
        }
        Session::set('uid', $payload['uid']->getValue());
        Session::set('userAgent', $userAgent);
        return $next($request);
    }
}